package com.lin.Demo;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lin.domain.Article;

/**
 * Created by chen on 2017/3/5.
 */
// Demo
class DemoFirst {
    public static void main(String args[]) throws  Exception{
        ObjectMapper mapper = new ObjectMapper();
        JacksonJsonFilter jacksonFilter = new JacksonJsonFilter();

        // 过滤除了 id,title 以外的所有字段，也就是序列化的时候，只包含 id 和 title
        String[] attribute = new String[]{"id,title"};
        jacksonFilter.include(Article.class, attribute);
        mapper.setFilterProvider(jacksonFilter);  // 设置过滤器
        mapper.addMixIn(Article.class, jacksonFilter.getClass()); // 为Article.class类应用过滤器
        String include= mapper.writeValueAsString(new Article());


        // 序列化所有字段，但是排除 id 和 title，也就是除了 id 和 title之外，其他字段都包含进 json
        jacksonFilter = new JacksonJsonFilter();
        jacksonFilter.filter(Article.class, attribute);
        mapper = new ObjectMapper();
        mapper.setFilterProvider(jacksonFilter);
        mapper.addMixIn(Article.class, jacksonFilter.getClass());

        String filter = mapper.writeValueAsString(new Article());

        System.out.println("include:" + include);
        System.out.println("filter :" + filter);
      /*  输出结果
        filterOut:{id: "", title: ""}
        serializeAll:{content:"",createTime:""}*/
    }
}
